﻿using Castle.DynamicProxy;
using CNKI.TPI.Web.Admin.Model;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Search.IBLL;
using CNKI.TPI.Web.Search.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CNKI.TPI.Web.UI.Controllers.Search
{
    public class TopicController : BaseController
    {
        //
        // GET: /Topic/
        private ITopicService topicService = null;
        private ISearchService searchService = null;
        private INaviService naviService = null;
        public TopicController(ITopicService subjectService, ISearchService searchService, INaviService naviService)
        {
            this.topicService = generator.CreateInterfaceProxyWithTarget<ITopicService>(subjectService, new IInterceptor[] { new LogInterceptor() });
            this.searchService = generator.CreateInterfaceProxyWithTarget<ISearchService>(searchService, new IInterceptor[] { new LogInterceptor() });
            this.naviService = generator.CreateInterfaceProxyWithTarget<INaviService>(naviService, new IInterceptor[] { new LogInterceptor() });
        }
        public ActionResult Index(string _topiccode)
        {
            string topiccode = _topiccode;
            string topicpage = "TopicSubjectIndex";
            IList<SYS_CMS_TOPIC_PAGECONFIG> pageInfo = topicService.GetPageConfig(topiccode);
            if (pageInfo != null && pageInfo.Count > 0)
            {
                pageInfo = pageInfo.Where(c => c.IsPub == 0).ToList();//取显示记录
                IList<SYS_CMS_TOPIC_PAGECONFIG> left = pageInfo.Where(c => c.ShowType == 4 || c.ShowType == 2 || c.ShowType == 3 || c.ShowType == 1).ToList();//左侧列表
                ViewBag.LeftList = left.OrderBy(c => c.SORTNO).ToList();
                ViewBag.NumLeft = left.Count;
                IList<SYS_CMS_TOPIC_PAGECONFIG> right = pageInfo.Where(c => c.ShowType == 5 || c.ShowType == 6 || c.ShowType == 7).ToList();//右侧列表
                ViewBag.RightList = right.OrderBy(c => c.SORTNO).ToList();
                ViewBag.NumRight = right.Count;
            }
            IList<SYS_CMS_TOPIC> topicInfo = topicService.GetTopicInfo(topiccode);
            if (topiccode != null && topicInfo.Count > 0)
            {
                ViewBag.TitlePic = topicInfo[0].TopicPic;
                ViewBag.TitleTopic = topicInfo[0].TopicName;
                ViewBag.CopyRight = topicInfo[0].CopyRight;
                ViewBag.Layout = "~/Views/Shared/_LayoutTopic" + topicInfo[0].TopicPage + ".cshtml";
            }
            IList<SYS_CMS_TOPIC_DBCONFIG> dbConfig = topicService.GetDBConfig(topicInfo[0].TopicDB.ToString());
            if (dbConfig != null && dbConfig.Count > 0)
            {
                ViewBag.DBID = dbConfig[0].DBID;
            }
            ViewBag.DateStr = System.DateTime.Now.ToString("yyyy-MM-dd") + "  " + System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek); ;
            ViewBag.TopicCode = topiccode;
            return View(topicpage);
        }
        [OutputCache(CacheProfile = "HtmlCache")]
        public ActionResult SubTopicPage(string dbCode, string nodeSysCode, string clsCode, int len, int pageSize, int showtype, string topiccode)
        {
            //SYS_CMS_TOPIC_CLS_QUERY query = topicService.GetDBTopicQuery(dbCode, clsCode, nodeSysCode);
            //SYS_CMS_DATABASE obj = naviService.GetDatabase(dbCode, 0);
            //if (query != null && obj != null)
            //{
            //    DataSet dsDoc = searchService.SingleSearch(obj.SysID, dbCode, null, 0, "", "", "", 1, pageSize, topicCLSCode: clsCode, topicCLSNodeCode: nodeSysCode, option: PermissionOption.None);
            //    if (dsDoc != null && dsDoc.Tables.Count > 0 && dsDoc.Tables[0].Rows.Count > 0)
            //    {

            //        ViewBag.SearchResultDoc=ColumnAliasHelper.GetContentNoRed(dsDoc.Tables[0]);
            //    }
            //    ViewBag.QueryObj = query;
            //}

            SYS_CMS_DATABASE obj = naviService.GetDatabase(dbCode, 0);
            DataSet dsDoc = searchService.SingleSearch(obj.SysID, dbCode, null, 0, "", "", "", 1, pageSize, topicCLSCode: clsCode, topicCLSNodeCode: nodeSysCode, option: PermissionOption.None);
            if (dsDoc != null && dsDoc.Tables.Count > 0 && dsDoc.Tables[0].Rows.Count > 0)
            {

                ViewBag.SearchResultDoc = ColumnAliasHelper.GetContentNoRed(dsDoc.Tables[0]);
            }
            ViewBag.QueryObj = null;

            ViewBag.Len = len;
            ViewBag.ShowType = showtype;
            ViewBag.TopicCode = topiccode;
            ViewBag.DbID = obj.SysID;
            ViewBag.DbName = obj.DatabaseCode;
            return PartialView("SubTopicPage");
        }
        /// <summary>
        /// 判断该专题下是否有栏目
        /// </summary>
        /// <param name="topiccode"></param>
        /// <returns></returns>
        public bool IsTopicHasDetial(string topiccode)
        {
            bool flage = false;
            IList<SYS_CMS_TOPIC_PAGECONFIG> pageInfo = topicService.GetPageConfig(topiccode);
            if (pageInfo != null && pageInfo.Count > 0)
            {
                flage = true;
            }
            return flage;
        }
        //
        // GET: /Topic/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Topic/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Topic/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Topic/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Topic/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Topic/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Topic/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
